Conditions | 1 |
Paths | 1 |
Total Lines | 97 |
Lines | 0 |
Ratio | 0 % |
Changes | 7 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | /** |
||
16 | function elements($timeout, state, data, util, visibility) { |
||
17 | let ct = this; |
||
18 | ct.state = state; |
||
19 | ct.data = data; |
||
20 | ct.util = util; |
||
21 | ct.outcome = {}; |
||
22 | ct.keys = Object.keys; |
||
23 | ct.buyAmount = [1, 10, 25, 100, 1000]; |
||
24 | let sortFunc = [ |
||
25 | (a,b) => data.elements[a].number - data.elements[b].number, |
||
26 | (a,b) => ct.getChance(b, state.player) - ct.getChance(a, state.player) |
||
27 | ] |
||
28 | |||
29 | ct.getChance = function(element, player) { |
||
30 | let bonus = 1; |
||
31 | for (let resource of data.elements[element].includes) { |
||
32 | let allTime = player.statistics.all_time[resource]; |
||
33 | if (allTime) { |
||
34 | bonus *= allTime * data.constants.ELEMENT_CHANCE_BONUS + 1; |
||
35 | } |
||
36 | } |
||
37 | |||
38 | let singleChance = data.elements[element].abundance * bonus; |
||
39 | let chance = 1 - Math.pow(Math.max(0, 1 - singleChance), |
||
40 | Math.min(player.resources.dark_matter, ct.buyAmount[player.options.elementBuyIndex])); |
||
41 | |||
42 | return Math.min(1, chance); |
||
43 | }; |
||
44 | |||
45 | ct.buyElement = function(element, player) { |
||
46 | if (player.elements[element]) { |
||
47 | return; |
||
48 | } |
||
49 | if (Math.random() < ct.getChance(element, player)) { |
||
50 | player.elements[element] = true; |
||
51 | player.exotic_upgrades[element] = {}; |
||
52 | for (let up in data.exotic_upgrades) { |
||
53 | player.exotic_upgrades[element][up] = false; |
||
54 | } |
||
55 | player.elements_unlocked++; |
||
56 | ct.outcome[element] = 'Success'; |
||
57 | } else { |
||
58 | ct.outcome[element] = 'Fail'; |
||
59 | } |
||
60 | player.resources.dark_matter -= Math.min(player.resources.dark_matter, ct.buyAmount[player.options.elementBuyIndex]); |
||
61 | |||
62 | util.delayedExec(performance.now(), performance.now(), 1000, () => ct.clearMessage(element)); |
||
|
|||
63 | }; |
||
64 | |||
65 | ct.clearMessage = function(element) { |
||
66 | ct.outcome[element] = ''; |
||
67 | }; |
||
68 | |||
69 | /* This function returns the class that determines on which |
||
70 | colour an element card */ |
||
71 | ct.elementClass = function(element, player) { |
||
72 | if (player.elements[element]) { |
||
73 | return 'element_purchased'; |
||
74 | } |
||
75 | if (isElementAvailable(element, player)) { |
||
76 | return 'element_available'; |
||
77 | } |
||
78 | return 'element_unavailable'; |
||
79 | }; |
||
80 | |||
81 | function isElementAvailable(element, player) { |
||
82 | for (let resource of data.elements[element].includes) { |
||
83 | if (player.resources[resource] !== null) { |
||
84 | return true; |
||
85 | } |
||
86 | } |
||
87 | return false; |
||
88 | } |
||
89 | |||
90 | /* This function returns the class that determines the secondary |
||
91 | colour of an element card */ |
||
92 | ct.elementSecondaryClass = function(element, player) { |
||
93 | return ct.elementClass(element, player) + '_dark'; |
||
94 | }; |
||
95 | |||
96 | ct.visibleTableElements = function(player) { |
||
97 | return visibility.visible(data.elements, isTableElementVisible, null, sortFunc[player.options.elementSortIndex], player); |
||
98 | }; |
||
99 | |||
100 | function isTableElementVisible(element, _, player) { |
||
101 | switch (ct.elementClass(element, player)) { |
||
102 | case 'element_purchased': |
||
103 | return !player.options.hideElementsPurchased; |
||
104 | case 'element_available': |
||
105 | return !player.options.hideElementsAvailable; |
||
106 | case 'element_unavailable': |
||
107 | return !player.options.hideElementsUnavailable; |
||
108 | default: |
||
109 | return true; |
||
110 | } |
||
111 | } |
||
112 | } |
||
113 |
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.